<template>
  <div class="app-container">
    <div style="margin:40px 0px;">
      <el-form ref="teacherQuery" :model="teacherQuery" :inline="true" label-width="80px">

        <el-form-item label="姓名">
          <el-input v-model="teacherQuery.name" />
        </el-form-item>

        <el-form-item label="职称级别">
          <el-select v-model="teacherQuery.level" placeholder="选择职称">
            <el-option label="高级讲师" value="1" />
            <el-option label="首席讲师" value="2" />
          </el-select>
        </el-form-item>

        <el-form-item label="活动时间">
          <el-col :span="11">
            <el-date-picker v-model="teacherQuery.begin" type="date" value-format="yyyy-MM-dd" placeholder="开始日期" style="width: 100%;" />
          </el-col>
          <el-col :span="2" class="line">--------</el-col>
          <el-col :span="11">
            <el-date-picker v-model="teacherQuery.end" type="date" value-format="yyyy-MM-dd" placeholder="结束日期" style="width: 100%;" />
          </el-col>
        </el-form-item>
        <el-button type="primary" @click="getTeacherPageData()">查询</el-button>
        <el-button type="primary" @click="clearTeacherPageData()">清空</el-button>
        <el-button type="primary" @click="tjSave()">添加教师数据</el-button>
      </el-form>
    </div>

    <el-table :data="list" stripe style="width: 100%">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="intro" label="讲师简介" width="180" />
      <el-table-column prop="career" label="讲师资历" />
      <el-table-column prop="name" label="头衔" width="180">
        <template slot-scope="scope">
          {{ scope.row.level==="1"?"高级讲师":"首席讲师" }}
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="80" />
      <el-table-column prop="gmtCreate" label="创建时间" />
      <el-table-column prop="gmtModified" label="修改时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <div class="block">
        <div style="">共有{{ total }}条数据</div>
        <el-pagination :total="total" :current-page.sync="current" :page-size="limit" :page-sizes="[5,10, 20, 30, 40]" layout="sizes, prev, pager, next" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import teacher from '@/api/edu/teacher.js'
export default {
  data() {
    return {
      // 当前页
      current: 1,
      // 一页多少个
      limit: 10,
      // 总数
      total: 0,
      teacherQuery: {},
      // 数据
      list: null
    }
  },
  watch: {},
  created() {
    this.getTeacherPageData()
  },
  methods: {
    // 获得讲师数据
    getTeacherPageData() {
      teacher
        .getTeacherPage(this.current, this.limit, this.teacherQuery)
        .then(response => {
          console.log(response.data)
          this.total = response.data.total
          this.list = response.data.rows
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 编辑教师数据
    handleEdit(id) {
      console.log(id)
      this.$router.push({ path: '/teacher/save/', query: { id }})
    },
    // 删除教师数据事件
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacher.deleteTeacher(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getTeacherPageData()
        })
      })
    },
    // 路由跳转
    tjSave() {
      this.$router.push('/teacher/save')
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
      this.getTeacherPageData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.current = val
      this.getTeacherPageData()
    },
    clearTeacherPageData() {
      // 清除数据.
      this.teacherQuery = {}
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}
</style>
